package it.bela.pgcdc.dao.impl;

import it.bela.pgcdc.common.bean.BaseBean;
import it.bela.pgcdc.common.dao.impl.AbstractBaseDao;
import it.bela.pgcdc.dao.ContattoDao;
import it.bela.pgcdc.domain.Attivita;
import it.bela.pgcdc.domain.Contatto;
import it.bela.pgcdc.domain.Hobby;
import it.bela.pgcdc.domain.MailingList;
import it.bela.pgcdc.domain.Preferenza;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ContattoDaoImpl extends AbstractBaseDao<Contatto> implements ContattoDao{

	private static Logger log = Logger.getLogger(ContattoDaoImpl.class);

	@Autowired(required = true)
	private HibernateTemplate hibernateTemplate;

	public List<Contatto> findByExample(Contatto contatto) {
		try{

			log.debug("Start findByExample Contatto");

			StringBuilder queryBuilder = new StringBuilder();
			List<String> paramNames = new ArrayList<String>();
			List<Object> values = new ArrayList<Object>();
			
			String[] paramNamesArray = null;
			BaseBean[] valuesArray = null;

			queryBuilder.append("select contatto from Contatto as contatto" +
					" where contatto.ownerUser = \'" + contatto.getOwnerUser() + "\'");

			if(contatto.getNome() != null && !contatto.getNome().equals("")){
				queryBuilder.append(" and contatto.nome = \'" + contatto.getNome() + "\'");
			}

			if(contatto.getCognome() != null && !contatto.getCognome().equals("")){
				queryBuilder.append(" and contatto.cognome = \'" + contatto.getCognome() + "\'");
			}

			if(contatto.getPreferenze() != null && !contatto.getPreferenze().isEmpty()){

				int indexItem = 0;
				for (Iterator<Preferenza> iterator = contatto.getPreferenze().iterator(); iterator.hasNext();) {
					Preferenza preferenza = (Preferenza) iterator.next();
					
					queryBuilder.append(" and :preferenze_item" + indexItem + " member of contatto.preferenze");
					
					paramNames.add("preferenze_item" + indexItem);
					values.add(preferenza);
					
					indexItem++;
				}
			}
			
			if(contatto.getMailingList() != null && !contatto.getMailingList().isEmpty()){

				int indexItem = 0;
				for (Iterator<MailingList> iterator = contatto.getMailingList().iterator(); iterator.hasNext();) {
					MailingList mailingList = (MailingList) iterator.next();
					
					queryBuilder.append(" and :mailingList_item" + indexItem + " member of contatto.mailingList");
					
					paramNames.add("mailingList_item" + indexItem);
					values.add(mailingList);
					
					indexItem++;
				}
			}
			
			if(contatto.getAttivita() != null && !contatto.getAttivita().isEmpty()){

				int indexItem = 0;
				for (Iterator<Attivita> iterator = contatto.getAttivita().iterator(); iterator.hasNext();) {
					Attivita attivita = (Attivita) iterator.next();
					
					queryBuilder.append(" and :attivita_item" + indexItem + " member of contatto.attivita");
					
					paramNames.add("attivita_item" + indexItem);
					values.add(attivita);
					
					indexItem++;
				}
			}
			
			if(contatto.getHobbies() != null && !contatto.getHobbies().isEmpty()){

				int indexItem = 0;
				for (Iterator<Hobby> iterator = contatto.getHobbies().iterator(); iterator.hasNext();) {
					Hobby hobby = (Hobby) iterator.next();
					
					queryBuilder.append(" and :hobbies_item" + indexItem + " member of contatto.hobbies");
					
					paramNames.add("hobbies_item" + indexItem);
					values.add(hobby);
					
					indexItem++;
				}
			}
			queryBuilder.append(" order by contatto.cognome asc");
			
			paramNamesArray = new String[paramNames.size()];
			valuesArray = new BaseBean[values.size()];

			@SuppressWarnings("unchecked")
			List<Contatto> list = hibernateTemplate.findByNamedParam(queryBuilder.toString(), paramNames.toArray(paramNamesArray), values.toArray(valuesArray));

			log.debug("Start findByExample Contatto");
			return list;

		} catch (RuntimeException re) {
			log.error("findByExample Contatto failed", re);
			throw re;
		}
	}
}